//
//  CaseManagerViewController.m
//  QuickCure
//
//  Created by haoge on 16/9/10.
//  Copyright © 2016年 haoge. All rights reserved.
//

#import "CaseManagerViewController.h"
#import "CaseManagerTableViewController.h"
#import "CaseManagerTableViewCell1.h"
#import "CaseManagerTableViewCell2.h"
#import <SVProgressHUD.h>
#import <Masonry.h>
//#import "CaseManagerTableViewCell1.h"
#import "Models.h"
@interface CaseManagerViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic ,copy)NSString *currentTime;
@property (nonatomic ,strong)NSMutableArray *nsArray;
@property (nonatomic ,weak)UITableView *scrollerView;
@property (nonatomic ,copy)NSString *filePath;

@property (nonatomic, weak) UIView *topView;

@end
static NSString *reuseID = @"hehe";
@implementation CaseManagerViewController

- (UIView *)topView{
    if (nil == _topView) {
        UIView *topView = [[UIView alloc] init];
        [self.view addSubview:topView];
        _topView = topView;
    }
    return _topView;
}

-(NSMutableArray*)nsArray
{
    if (_nsArray == nil) {
        _nsArray = [NSMutableArray array];
    }
    return _nsArray;
}


//-(void)viewWillAppear:(BOOL)animated
//{
//    [self.scrollerView reloadData];
//}
- (void)viewDidLoad
{
    [super viewDidLoad];
    //上面的视图
    [self setupTopView];
    //中间的视图
    [self setupBottomView];
    //下面的视图
    [self setupButton];
    //数据持久化
    [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0] stringByAppendingPathComponent:@"aa.data"];
    self.nsArray = [NSKeyedUnarchiver unarchiveObjectWithFile:[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0] stringByAppendingPathComponent:@"aa.data"]];
    
//    NSLog(@"%@",);
    
}
//设置上面的视图
-(void)setupTopView
{
    //头部View
    UIView *topView = [[UIView alloc] init];
    [self.view addSubview:topView];
    self.topView = topView;
    
    UILabel *pName = [[UILabel alloc] init];
    pName.text = @"小石头";
    pName.font = [UIFont systemFontOfSize:19];
    [self.topView addSubview:pName];
    
    [pName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.topView).multipliedBy(0.35);
        make.centerY.equalTo(self.topView).multipliedBy(1.65);;
    }];
    
    UIImageView *userPhoto = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"image副本"]];
    [self.topView addSubview:userPhoto];
    
    [userPhoto mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(pName);
        make.centerY.equalTo(self.topView).multipliedBy(0.8);
        make.width.height.equalTo(@70);
    }];
    
    UIImageView *image1 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"0"]];
    [self.topView addSubview:image1];
    
    UIImageView *image2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"idCard"]];
    [self.topView addSubview:image2];
    
    UIImageView *image3 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"phone"]];
    [self.topView addSubview:image3];
    
    UIImageView *image4 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"illness_rb_img_sel.png"]];
    [self.topView addSubview:image4];
    
    UILabel *label1 = [[UILabel alloc] init];
    label1.text = @"男    28";
    label1.font = [UIFont systemFontOfSize:18];
    [self.topView addSubview:label1];
    
    UILabel *label2 = [[UILabel alloc] init];
    label2.text = @"111111111111111111";
    label2.font = [UIFont systemFontOfSize:18];
    [self.topView addSubview:label2];
    
    UILabel *label3 = [[UILabel alloc] init];
    label3.text = @"222222222222222222";
    label3.font = [UIFont systemFontOfSize:18];
    [self.topView addSubview:label3];
    
    UILabel *label4 = [[UILabel alloc] init];
    label4.text = @"上海市";
    label4.font = [UIFont systemFontOfSize:18];
    [self.topView addSubview:label4];
    
   
    
    [image1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(pName.mas_trailing).offset(23);
        make.centerY.equalTo(self.topView).multipliedBy(0.4);
        make.width.height.equalTo(@17);
    }];
    [image2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(image1);
        make.centerY.equalTo(self.topView).multipliedBy(0.8);
        make.width.height.equalTo(@17);
    }];
    [image3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(image1);
        make.centerY.equalTo(self.topView).multipliedBy(1.2);
        make.height.equalTo(@18);
        make.width.equalTo(@15);
    }];
    [image4 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(image1);
        make.centerY.equalTo(self.topView).multipliedBy(1.6);
        make.width.height.equalTo(@17);
    }];
    
    [label1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(image1.mas_trailing).offset(11);
        make.centerY.equalTo(image1);
    }];
    [label2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(label1);
        make.centerY.equalTo(image2);
    }];
    [label3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(label1);
        make.centerY.equalTo(image3);
    }];
    [label4 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(label1);
        make.centerY.equalTo(image4);
    }];
    
    //头部View约束
    [topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.equalTo(self.view);
        make.top.equalTo(self.view).offset(64);
        make.height.equalTo(self.view).multipliedBy(0.25);
    }];
    
    //灰色view
    UIView *lineView = [[UIView alloc] init];
    lineView.backgroundColor = [UIColor grayColor];
    [self.view addSubview:lineView];
    
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.equalTo(self.view);
        make.top.equalTo(topView.mas_bottom);
        make.height.equalTo(@1);
    }];
    
}
//设置中间的视图
-(void)setupBottomView
{
    UITableView *tableView = [[UITableView alloc]init];
    self.scrollerView = tableView;
    tableView.dataSource = self;
    tableView.delegate = self;
    tableView.rowHeight = 130;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:reuseID];
    [self.view addSubview:tableView];
    
    [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.equalTo(self.view);
        make.top.equalTo(self.topView.mas_bottom).offset(2);
        make.bottom.equalTo(self.view).offset(-47);
    }];
}
//设置下面的视图
-(void)setupButton
{
    UIView *view1 = [[UIView alloc]initWithFrame:CGRectMake(0, 614, self.view.frame.size.width, 47)];
    UIButton *button = [[UIButton alloc]init];
    [button setTitle:@"添加病例" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    button.backgroundColor = [UIColor colorWithRed:0.1333 green:0.7804 blue:0.7804 alpha:1.0];;
    button.layer.cornerRadius = 3;
    button.clipsToBounds = YES;
    [button addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [view1 addSubview:button];
    [self.view addSubview:view1];
    
    [button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(view1).offset(5);
        make.leading.equalTo(view1).offset(12);
        make.trailing.equalTo(view1).offset(-12);
        make.bottom.equalTo(view1).offset(-5);
    }];
    
}
//下面的按钮点击跳转下一个页面
-(void)btnClick:(UIButton*)button
{
    CaseManagerTableViewController *caseManagerTableViewController = [[CaseManagerTableViewController alloc]init];
    caseManagerTableViewController.title = @"增加病例";
    caseManagerTableViewController.block11 = ^(NSDictionary*dict)
    {
        // NSLog(@"nsarray11%@",nsArray11);
        [self.nsArray addObject: dict];
        // NSLog(@"=========%@",dict);
        NSEnumerator *enumerator = [self.nsArray reverseObjectEnumerator];
        self.nsArray = (NSMutableArray*)[enumerator allObjects];
        [self.scrollerView reloadData];
        //        [self.nsArray writeToFile:self.filePath atomically:YES];
        //NSLog(@"--------%@",self.nsArray);
        //NSLog(@"%lu",self.nsArray.count);
    };
    
    
    [self.navigationController pushViewController:caseManagerTableViewController animated:NO];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.nsArray.count;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    //UITableViewCell *cell = [[UITableViewCell alloc]init];
    NSDictionary *dic = self.nsArray[indexPath.row];
    Models *models = [[Models alloc]init];
    models.textFieldContent = dic[@"textFieldContent"];
    NSLog(@"%@",models.textFieldContent);
    models.detailText1 = dic[@"detailText1"];
    NSLog(@"%@",models.detailText1);
    models.detailText2 = dic[@"detailText2"];
    models.currentTime = dic[@"currentTime"];
    models.image = dic[@"image"];
    
    if (models.image == nil)
    {
        CaseManagerTableViewCell2 *cell = [[CaseManagerTableViewCell2 alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.models = models;
        return cell;
    }else
    {
        CaseManagerTableViewCell1 *cell = [[CaseManagerTableViewCell1 alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
        cell.models = models;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
}

#pragma mark
#pragma mark -- 点击cell触发事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [self showSuccessInfo];
}

#pragma mark
#pragma mark -- SVProgressHUD 设置弹窗效果
- (void)showSuccessInfo {
    
    UIImage *image = [UIImage imageNamed:@"SVProgressHUD.bundle/info"];
    
    [SVProgressHUD showImage:image status:@"正在开发中"];
    
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
    
    //设置消失时间
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        // time-consuming task
        [NSThread sleepForTimeInterval:1.5];
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [SVProgressHUD dismiss];
        });
    });
}

#pragma mark
#pragma mark -- 删除表格
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    
    //1. 数据都存在在数组中
    NSMutableArray *nmArray =  self.nsArray;
    //2.删除数组中的数据
    [nmArray removeObjectAtIndex:indexPath.row];
    
    self.nsArray = nmArray;
    //3. 刷新表格

    [self.scrollerView reloadData];
}

#pragma mark
#pragma mark -- 在界面消失后本地数据持久化
- (void)viewDidDisappear:(BOOL)animated{
    if(self.nsArray.count != 0)
    {
        [NSKeyedArchiver archiveRootObject:self.nsArray toFile:[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0] stringByAppendingPathComponent:@"aa.data"]];
    }
}

@end

